//
// Created by wangjunjun on 2024/4/16.
//

#include "subcmd.h"
#include "../log/log.h"
std::shared_ptr<CSubCmd> CSubCmd::m_Instance= nullptr;
std::once_flag CSubCmd::m_flag;

CSubCmd::CSubCmd(){

}
CSubCmd::~CSubCmd(){

}

std::shared_ptr<CSubCmd> CSubCmd::GetInStance() {
    std::call_once(m_flag, []() {
        m_Instance = std::shared_ptr<CSubCmd>(new CSubCmd(),
                                              [](CSubCmd* ptr){delete ptr;});
    });
    return m_Instance;
}
/**
 * @brief:对LCM
 * */
void CSubCmd::Start() {

    std::thread lcmThread(&CSubCmd::HandRecProc,this);
    lcmThread.detach();

}
void  CSubCmd::HandleMessage(const lcm::ReceiveBuffer *rbuf, const std::string &channel, const CTokenOverType *msg) {
    //接收到数据通过MQTT发送出去
   // CMqttClient::GetInStance()->SendTakeOverMsg("TakeOver", msg->MsgType);
}
void CSubCmd::HandRecProc(){
    lcm::LCM lcm;
    if(!lcm.good()){
        LOG_ERROR("LCM init failed!");
        return;
    }
    CSubCmd Handler;
    //订阅消息
    lcm.subscribe("TakeOver", &CSubCmd::HandleMessage, &Handler);
    while (true){
        lcm.handle();
    }
}